package Components
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.core.LayoutContainer;
	import mx.effects.Rotate;
	import mx.events.FlexEvent;

	public class EdgeScrollableContainer extends LayoutContainer
	{
		public var scrollZoneWidth:int = 100;
		public var centerChild:Boolean = false;
		
		public var showArrows:Boolean = true;
		
		[Embed(source='../assets/rightArrow.png')]
		protected var arrow:Class;
		
		protected var leftArrow:Image;
		protected var rightArrow:Image;
		
		public function EdgeScrollableContainer()
		{
			super();
			horizontalScrollPolicy = "off";
			verticalScrollPolicy = "off";
			
			//attach to the top level system manager, which ensures
			//	we get ALL MouseMove events.
			addEventListener(FlexEvent.CREATION_COMPLETE, function(evt:Event):void
			{
				systemManager.topLevelSystemManager.addEventListener(MouseEvent.MOUSE_MOVE,
								mouseMoveHandler);
			});
		}
		
		protected override function createChildren():void
		{
			super.createChildren();
			if(!leftArrow)
			{
				leftArrow = new Image();
				leftArrow.source = new arrow();
				leftArrow.x = 100;
				leftArrow.y = (height-100)/2;
				leftArrow.width = 100;
				leftArrow.height = 100;
				leftArrow.alpha = 0.2;
				rawChildren.addChild(leftArrow);
				
				leftArrow.scaleX = -leftArrow.scaleX;
			}
			if(!rightArrow)
			{
				rightArrow = new Image();
				rightArrow.source = new arrow();
				//rightArrow.x = width - 50;
				rightArrow.y = (height-100)/2;
				rightArrow.width = 100;
				rightArrow.height = 100;
				rightArrow.alpha = 0.2;
				rawChildren.addChild(rightArrow);
			}
		}
		
		protected function mouseMoveHandler(evt:MouseEvent):void
		{
			if(mouseX < scrollZoneWidth)
			{
				var scrollAmount:int = scrollZoneWidth - mouseX;
				scrollAmount = Math.min(scrollAmount, horizontalScrollPosition);
				horizontalScrollPosition -= scrollAmount;
			}
			if(mouseX > width-scrollZoneWidth)
			{
				scrollAmount = scrollZoneWidth-width+mouseX;
				scrollAmount = Math.min(maxHorizontalScrollPosition, scrollAmount);
				horizontalScrollPosition += scrollAmount;
			}
		}
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			leftArrow.y = (height-scrollZoneWidth)/2;
			rightArrow.x = width - scrollZoneWidth + 5;
			rightArrow.y = (height-scrollZoneWidth)/2;
			
			if(horizontalScrollPosition > 0)
				leftArrow.visible = true;
			else
				leftArrow.visible = false;
			
			if(horizontalScrollPosition < maxHorizontalScrollPosition)
				rightArrow.visible = true;
			else
				rightArrow.visible = false;
			
			//this is a hack to work around bug SDK-12682
			if(centerChild && numChildren > 0)
			{
				var ch:DisplayObject = getChildAt(0);
				if(ch.width < width)
					ch.x = (width/2)-(ch.width/2);
				else
					ch.x = 0;
			}
		}
	}
}